#include "taskQueue.h"

int taskQueueInit(taskQueue_t *pTaskQueue)
{
    memset(pTaskQueue, 0, sizeof(taskQueue_t));
    return 0;
}

int printTaskQueue(taskQueue_t *pTaskQueue)
{
    taskNode_t *pCurNode = pTaskQueue->head;
    while(pCurNode)
    {
        printf("%d", pCurNode->netfd);
        if(pCurNode->next)
        {
            printf("->");
        }
        pCurNode = pCurNode->next;
    }
    printf("\n");
    return 0;
}

int enQueue(taskQueue_t *pTaskQueue, int ntfd)
{
    taskNode_t *pNewNode = (taskNode_t *)calloc(1, sizeof(taskNode_t));
    pNewNode->netfd = ntfd;
    ERROR_CHECK(pNewNode, NULL, "calloc newNode");
    if(pTaskQueue->queueSize == 0)
    {
        pTaskQueue->head = pNewNode;
    }
    else
    {
        pTaskQueue->tail->next = pNewNode;
    }
    pTaskQueue->tail = pNewNode;
    ++pTaskQueue->queueSize;
    return 0;
}

int deQueue(taskQueue_t *pTaskQueue)
{
    taskNode_t *pCurNode = pTaskQueue->head;
    pTaskQueue->head = pCurNode->next;
    if(pTaskQueue->queueSize == 1)
    {
        pTaskQueue->tail = NULL;
    }
    free(pCurNode);
    --pTaskQueue->queueSize;
    return 0;
}
